package com.xiaoke.service.impl;

import com.xiaoke.entity.Cart;
import com.xiaoke.mapper.CartMapper;
import com.xiaoke.service.CartService;
import com.xiaoke.utils.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    @Transactional(readOnly = false)
    public Cart selectByPrimaryKey(String id) {
        return cartMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public void insert(@RequestBody Cart cart) {
        if(StringUtils.isBlank(cart.getId()))
            cart.setId(String.valueOf(snowflakeIdWorker.nextId()));
        cartMapper.insert(cart);
    }

    @Override
    public List<Map<String, Object>> findByUserAndShop(String userId, String shopId) {
        return cartMapper.findByUserAndShop(userId,shopId);
    }

    @Override
    public Cart findByParams(@RequestBody Map<String, Object> params) {
        return cartMapper.findByParams(params);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateByPrimaryKeySelective(@RequestBody  Cart cart) {
        cartMapper.updateByPrimaryKeySelective(cart);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(@RequestBody String... id) {
        cartMapper.deleteByIds(id);
    }

    @Override
    public BigDecimal sumByChecked(String userId, String shopId) {
        return cartMapper.sumByChecked(userId,shopId);
    }

    @Override
    public List<Map<String, Object>> findByChecked(String userId, String shopId) {
        return cartMapper.findByChecked(userId,shopId);
    }

    @Override
    public Map<String, Object> sumTotalPriceByChecked(String userId, String shopId) {
        return cartMapper.sumTotalPriceByChecked(userId,shopId);
    }

    @Override
    public Integer countByUserAndShopAndUnChecked(String userId, String shopId) {
        return cartMapper.countByUserAndShopAndUnChecked(userId,shopId);
    }
}
